<?php

use Carbon\Carbon;

if (!function_exists('data_cycles_from_date')) {
    function data_cycles_from_date($dateStr)
    {
        $date = null;
        if ($dateStr != null) {
            $dateStr = substr($dateStr, 0, 7);
            $dateStr = $dateStr . '-02';
            $date = Carbon::createFromFormat('Y-m-d', $dateStr)->lastOfMonth();
        } else {
            $date = now();
            $date = $date->subMonth()->lastOfMonth();
        }

        $data_cycle = array();
        $data_cycle['month'] = Carbon::create($date->year,$date->month,01)->lastOfMonth();
        $data_cycle['quarter'] = Carbon::create($date->year, $date->month, 01)->subQuarter()->lastOfQuarter();
        if ($date->month > 6) {
            $data_cycle['half_year'] = Carbon::create($date->year,6,1)->lastOfMonth();
        }else{
            $data_cycle['half_year'] = Carbon::create($date->year,$date->month,$date->day)->subYear()->lastOfYear();
        }
        $data_cycle['year'] = Carbon::create($date->year)->subYear()->lastOfYear();

        return $data_cycle;
    }
};
